#include "controlador.h"

Controlador::Controlador(QObject *parent) : QObject(parent)
{
    //    tcpServer = new QTcpServer(this);
        vetorDeThread = new QVector<ThreadTrataCliente *>();
        mini  = new MiniControlador();

}
void Controlador::startServer(){

        tcpServer = new QTcpServer(this);

        if (!tcpServer->listen(QHostAddress::Any, 55565 )) {
                qDebug()<<"Servidor não está escutando";
        }
        connect(tcpServer, SIGNAL(newConnection()), this, SLOT(novoCliente()));
        qDebug()<<"iniciou servidor";
}
void Controlador::novoCliente(){

   ThreadTrataCliente * thread = new ThreadTrataCliente(tcpServer->nextPendingConnection(),mini);
  // QThread * novaThread = new QThread();
   //thread->moveToThread(novaThread);
   thread->run();
   //novaThread->start();
   vetorDeThread->append(thread);

//   QByteArray block;
//   QDataStream out(&block, QIODevice::WriteOnly);
//   out.setVersion(QDataStream::Qt_4_0);
//   out << (quint16)0;
//   out <<QString("mensagemmm");
//   out.device()->seek(0);
//   out << (quint16)(block.size() - sizeof(quint16));

//   QTcpSocket *clientConnection = tcpServer->nextPendingConnection();
//   connect(clientConnection, SIGNAL(disconnected()),
//           clientConnection, SLOT(deleteLater()));

//   clientConnection->write(block);
//   clientConnection->disconnectFromHost();
//   qDebug()<<"enviou";
}
